unit hademapbase;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  SysUtils,
  contnrs;

type
  { THadeSimpleList }

  THadeSimpleList = class
  protected
    Flist:TfpObjectList;
  public
    function count:integer;

    constructor create;virtual;
    destructor destroy;override;
  end;

  { THadeMapBase }

  THadeMapBase = class(TFPHashObjectList)
  protected
    function IsKeyExists(const aKey: string): boolean;
  public
    function add(const AName: shortstring; const AObject: TObject): integer; virtual;
    function Find(const s: shortstring): TObject; virtual;
  end;

  { THadeSimpleValue }

  THadeSimpleValue = class
  private
    FValue: variant;
  public
    constructor Create(AValue: variant);
    property Value: variant read FValue write FValue;
  end;

  { THadeMapSimpleValue }

  THadeMapSimpleValue = class(THadeMapBase)
  protected
    function getItem(index: integer): THadeSimpleValue; reintroduce;
  public
    property Items[index: integer]: THadeSimpleValue read getItem;
    function find(const s: shortstring): THadeSimpleValue; reintroduce;
  end;

implementation

{ THadeSimpleList }

function THadeSimpleList.count: integer;
begin
  Result := FList.count;
end;

constructor THadeSimpleList.create;
begin
  Flist:=TFpObjectList.Create(TRUE);
end;

destructor THadeSimpleList.destroy;
begin
  FList.Pack;
  FreeAndNil(FList);
  inherited destroy;
end;

{ THadeMapSimpleValue }

function THadeMapSimpleValue.getItem(index: integer): THadeSimpleValue;
begin
  Result := THadeSimpleValue(inherited getItem(index));
end;

function THadeMapSimpleValue.find(const s: shortstring): THadeSimpleValue;
begin
  Result := THadeSimpleValue(inherited find(s));
end;

{ THadeSimpleValue }
constructor THadeSimpleValue.Create(AValue: variant);
begin
  Value := AValue;
end;

{ THadeMapBase }

function THadeMapBase.IsKeyExists(const aKey: string): boolean;
begin
  Result := False;
  if Find(AKey) <> nil then
    Result := True;
end;

function THadeMapBase.add(const AName: shortstring; const AObject: TObject): integer;
begin
  Result := -1;

  if IsKeyExists(AName) then
  begin
    AObject.Free;
    Exit;//==>
  end;

  Result := inherited Add(UpperCase(AName), AObject);
end;

function THadeMapBase.Find(const s: shortstring): TObject;
begin
  Result := inherited find(UpperCase(s));
end;

end.

